require 'net/smtp'
class AdminController < ApplicationController
  
  def newBook 
    @newbook= Book.new
  end
  
  def createbook
    @book= Book.new(params[:newbook])
    @book.Available_quantity=@book.Quantity
    if @book.save
       
      redirect_to :action => 'book_manager',:alert1 => @book.Name+" is added"
     else
       render :action => 'newbook'
    end
  end
  
 def book_manager
   @bam = 1
      if session[:show].nil?
        @result=Book.all
      else
        @result=session[:show]
      end
      if session[:nr].nil?
        session[:nr]=@result.length
      end
      @notice=session[:nr].to_s + " results"
      if (session[:page].nil?)
        session[:page]=0
      end
      @from=session[:page].to_i*10
      @to=session[:page].to_i*10+9
      if (@to>=session[:nr].to_i)
        @to=session[:nr].to_i-1
      end
      session[:show]=nil
     
  end
  
  def search_string(a,b)
    return (a.upcase.strip==b.upcase.strip)
  end
  
  def search_process
    
   @all=Book.all
    @results=Array.new()
    @count=0
    @all.each do |r|
      if params[:id].empty? or r.id.to_i==params[:id].to_i
        if params[:name].empty? or search_string(r.Name.to_s,params[:name].to_s)==true
          if params[:author].empty? or search_string(r.Author.to_s,params[:author].to_s)==true
            if params[:genre].empty? or search_string(r.Genre.to_s,params[:genre].to_s)==true
              if params[:publish_year].empty? or r.Publish_year.to_i==params[:publish_year].to_i 
                @results << r
                @count=@count+1
              end
            end
          end
        end
      end 
    end 
    
    session[:show]=@results
    session[:nr]=@count
    session[:page]=0
    redirect_to :action => 'book_manager'
  end
  
  def change_page
    session[:page]=session[:page].to_i+params[:type].to_i
    if (session[:page].to_i<0) 
      session[:page]=0
    end
    if (session[:page].to_i*10>=session[:nr].to_i)
      session[:page]=session[:page].to_i-1
    end
    redirect_to :action=>'book_manager'
  end
  
  def editbook
    @book = Book.find(params[:id])
  end
  
  def updatebook
    @book = Book.find(params[:id])
      if @book.update_attributes(params[:book])
         redirect_to :action => 'book_manager',:alert1 => @book.Name+" has been updated"
      else
         @book = Book.find(:all)
         render :action => 'editbook'
      end
  end
  
   def deletebook
      Book.find(params[:id]).destroy
      redirect_to :action => 'book_manager' ,:alert1 => "One book is deleted"
   end
   
   def borrowreport
     @bam = 3
    if session[:showb].nil?
        @result=nil
      else
        @result=session[:showb]
        
      end
      session[:showb]=nil
     
      
  end
  
  def searchborrow_process
    @book=Book.find_by_Name(params[:bname])
    @user=User.find_by_Username(params[:uname])
    if @user != nil
      if @book != nil
        @br=Borrowingreport.find_all_by_Uid_and_Bid_and_Status(@user.id,@book.id,["Order","dangmuon"])
      else
        @br=Borrowingreport.find_all_by_Uid_and_Status(@user.id,["Order","dangmuon"])
      end
    else
      if @book != nil
        @br=Borrowingreport.find_all_by_Bid_and_Status(@book.id,["Order","dangmuon"])
      else
        @br=nil
      end
    end
   
   session[:showb]=@br
    redirect_to :action => "borrowreport"
  end
  
  def insertborrow
    @book=Book.find_by_Name(params[:bname])
    @user=User.find_by_Username(params[:uname])
    @a=Borrowingreport.find_by_Uid_and_Bid(@user.id,@book.id)
    #@a=Borrowingreport.find_by_U(@user.id)
    #@a=Borrowingreport.find_by_Username_and_Name(@user.Username,@book.Name)
      if @a==nil or @a.Return_date!=nil
        
      
        @date=Borrowingreport.new
        @date.Uid=@user.id
        @date.Bid=@book.id
       @date.Status="dangmuon"
       @date.Borrow_date=(Time.now).to_s(:db)
        @date.Deadline=(Time.now+10.day).to_s(:db)
       @date.isExtend=0
         @date.save
        @book.update_attributes(:Available_quantity => @book.Available_quantity-1)
         redirect_to :action => "borrowreport", :alert1 => "Update successful"
         return
      else
        redirect_to :action => "borrowreport", :alert => @user.Name+ " can't borrow " +@book.Name
     end
  end
  
  def trasach
    @br=Borrowingreport.find(params[:boid])
    @boo=Book.find_by_id(@br.Bid)
    @br.Status="damuon"
    @br.Return_date=(Time.now).to_s(:db)
    
    @boo.update_attributes(:Available_quantity => @boo.Available_quantity+1)
    @br.save
    
     redirect_to :action => "borrowreport", :alert1 => "Update successful"
  end
    
  def muonsach
    @br=Borrowingreport.find(params[:boid])
    @br.Status="dangmuon"
    @br.Borrow_date=(Time.now).to_s(:db)
    @br.Deadline=(Time.now+10.day).to_s(:db)
    @br.save
    redirect_to :action => "borrowreport", :alert1 => "Update successful"
  end  
  
  def newuser 
    @bam = 2
    @newUser= User.new
    @bang=User.all
  end
  
  def createuser
    @User= User.new(params[:newuser])
     @User.Username = @User.Name;
      @User.Password = @User.Name;
    if @User.save
      redirect_to :action => 'newuser'
     else
       render :action => 'newuser'
    end
  end
  
  def adminservice
     @bam = 4
     @cl=Servicereport.find_all_by_Status("order")
  end 
  
    def message
    @bam = 5
    @allmess = Message.find(:all, :conditions=>'Type=0 or Type=3')  #0 la user -> admin #1 la admin -> user
    if(!session[:mesdetail].nil?)
      @detail= session[:mesdetail]
    end
    
  end
  def message_detailsbe
    @md = Message.find_by_id(params[:mes])
    session[:mesdetail]=@md
    redirect_to :action=> 'message'
  end
  def reply
    @md = session[:mesdetail]
    @newm = Message.new
    @newm.Title = 'Re '+ @md.Title
    @newm.Date_sent = Time.now
    @newm.Uid = @md.Uid
    @newm.Content = params[:repmess]
    @newm.Type = 1
    @md.Type = 3
    @md.save
    @newm.save
    session[:mesdetail]=nil
    
    redirect_to :action=> 'message'
  end
  
  
  def sproc
   session[:serprocess]=params[:sid]
   
    redirect_to :action=> 'adminservice'
  end
  
  def replyservice
     @ll=Servicereport.find_by_id(session[:serprocess])
     @ll.Status="processs"
     @ll.save
     session[:serprocess]=nil
      redirect_to :action=> 'adminservice'
  end
  
      def email(to,bd,opts={})
  opts[:server]      = 'smtp.mail.yahoo.com'
  opts[:from]        = 'acsxr6r5@yahoo.com'
  opts[:from_alias]  = 'acsxr6r5@yahoo.com'
  opts[:subject]     = "................"
  opts[:body]        = bd

  msg = <<END_OF_MESSAGE
From: #{opts[:from_alias]} <#{opts[:from]}>
To: <#{to}>
Subject: #{opts[:subject]}

#{opts[:body]}
END_OF_MESSAGE

  Net::SMTP.start('smtp.mail.yahoo.com', 25, 'acsxr6r5@yahoo.com','acsxr6r5', '123456', :login) do |smtp|
        smtp.send_message msg, 'acsxr6r5@yahoo.com', to
end
  
end
  
end